import 'package:flutter/material.dart';

class ClipRectExample extends StatefulWidget {
  const ClipRectExample({Key? key}) : super(key: key);

  @override
  State<ClipRectExample> createState() => _ClipRectExampleState();
}

class _ClipRectExampleState extends State<ClipRectExample> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ClipRectExample'),
      ),
      body: Center(
        child: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              // ClipRect(
              //   /// 自定义裁剪路径
              //   clipper: ClipperPath(),
              //   child: Align(
              //     alignment: Alignment.center,
              //     widthFactor: 0.5,
              //     heightFactor: 0.5,
              //     child: Image(
              //       image: AssetImage("assets/images/lock.png"),
              //       fit: BoxFit.fill,
              //     ),
              //   ),
              // ),
              // ClipRRect(
              //   borderRadius: BorderRadius.all(Radius.circular(20)),
              //   child: Container(
              //     color: Colors.purple,
              //     width: 300,
              //     height: 300,
              //   ),
              // ),
              ClipRRect(
                clipper: ClipperRPath(),
                child: Container(
                  color: Colors.purple,
                  width: 300,
                  height: 300,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class ClipperPath extends CustomClipper<Rect> {
  @override
  Rect getClip(Size size) {
    return Rect.fromLTRB(0, 0, size.width, size.height);
  }

  @override
  bool shouldReclip(covariant CustomClipper<Rect> oldClipper) {
    return true;
  }
}

class ClipperRPath extends CustomClipper<RRect> {
  @override
  RRect getClip(Size size) {
    return RRect.fromRectAndCorners(
      Rect.fromCenter(
        center: Offset(150, 100),
        width: 300,
        height: 100,
      ),
      topLeft: Radius.circular(50),
      bottomRight: Radius.circular(50),
    );
    // return RRect.fromLTRBR(
    //   0,
    //   0,
    //   300,
    //   300,
    //   Radius.circular(30.0),
    // );
  }

  @override
  bool shouldReclip(covariant CustomClipper<RRect> oldClipper) {
    return true;
  }
}
